<?php
/**
 * 后台基类控制器
 * @author Administrator
 *
 */
class AdminController extends BaseController{
	public $layout='/layouts/main';
	public $pagesize=15;
	public $defaultAction='index';
	public $config=array();
	public $userinfo;
    protected $_request;
	
	public function init(){
		parent::init();        
		$this->pageTitle = Yii::app()->params['name'];  
        $this->_request = new CHttpRequest();     
	}
	public function filters()
	{
		return array(
			'accessControl', //perform access control for CRUD operations
		);
	}
	//权限控制
	public function accessRules()
	{
		//数组从上到下执行，只要一满足条件就表示允许访问
	    $accessRules = array(
	            array('allow',  //所有用户都允许访问
	                  'actions'=>array('captcha','edit'),
	                  'users'=>array('*'),
	            ),
	            array('deny',//所有用户都不允许访问
	                  'users'=>array('*')
	            )
	    );
	    $allowRules = array();
        $aModule = explode('/' ,$this->module->id);
        $sModuleId = count($aModule) > 1 ? end($aModule) : $aModule[0];
        if(Yii::app()->user->checkAccess($sModuleId .'|'.$this->id.'|'.$this->action->id)){
            
            $allowRules = array(
                    array('allow', //所有用户都必须要登录才能访问
                            'actions'=>array($this->action->id),
                            'users'=>array('@'),
                    )
            );
        }
	    //echo '<pre>';print_r(CMap::mergeArray($allowRules, $accessRules));exit;
	    return CMap::mergeArray($allowRules, $accessRules);
	}
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'foreColor'=>0x286345,
				'backColor'=>0xCDCBBD,
				'maxLength'=>4,
				'minLength'=>4,
				'height'=>29,
				'width'=>70,
				//'fontFile'=>'plugins/font/elephant.ttf',
				'padding'=>2,
				'testLimit'=>1,
				'offset'=>-1,
				//'transparent'=>'10%',
			),
		);
	}
	/**
	 * 返回ajax的json数据
	 *
	 * @param unknown_type $num 0成功 1失败
	 * @param unknown_type $msg
	 */
	public function ajaxReturn($err,$msg='' ,$url='' ,$data=array()){
		$arr=array(
			'err' => $err,
			'msg' => $msg,
            'url' =>$url ,
            'data' => $data
		);
		die(json_encode($arr));
	}
	/**
	 * 信息提示中间页面
	 * @param int    $num 错误号 0成功 1失败
	 * @param string $Msg 提示信息
	 * @param int    $waitTime 等待时间
	 * @param string $defaultPage 跳转页面
	 */
	public function showMessage($num,$Msg='',$redirectUrl='',$waitTime=3){ // 提示图标、提示语句、默认跳转的页面、时间
	    /* if(Yii::app()->request->isAjaxRequest) {
	        $arr=array(
	                'err'=>$num,
	                'msg'=>$Msg,
	                'defaultPage'=>$defaultPage,
	                'data'=>$data
	        );
	        die(json_encode($arr));
	    } */
	    $this->render('/layouts/showmessage',array(
			'num'=>$num,
			'Msg'=>$Msg,
			'redirectUrl'=>$redirectUrl,
			'waitTime'=>$waitTime,
		));
		Yii::app()->end();
	}
}
?>